<template>
  <div>
    <el-tree ref="treeRef" node-key="id" show-checkbox :check-strictly="true" default-expand-all="" :data="list" :props="{label:'name'}" />
    <el-row type="flex" justify="end" style="margin-top:15px">
      <el-button type="primary" @click="submitS">确认</el-button>
    </el-row>
  </div>
</template>

<script>
import { queryAllPermissions } from '@/api/permissions'
import { queryRoleInfoById, saveRolePermiss } from '@/api/settings'

import { array2Tree } from '@/utils'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      permIds: []
    }
  },
  created() {
    this.getAllPermission()
  },
  methods: {
    // !1.获取到所有的权限信息
    async getAllPermission() {
      const res = await queryAllPermissions()
      // console.log(res)
      // 将权限点数据 转换成树数据
      this.list = array2Tree(res.data, '0')
    },
    // !2根据指定id获取对应的角色信息
    async getRoleInfoById() {
      const res = await queryRoleInfoById(this.curId)
      // console.log(res)
      // 调用tree的方法 tree组件需要添加 属性node-key="id"
      this.$refs.treeRef.setCheckedKeys(res.data.permIds)
    },
    // 点击确认 保存
    async submitS() {
      this.permIds = this.$refs.treeRef.getCheckedKeys()
      // console.log(this.permIds)
      try {
        const res = await saveRolePermiss({ id: this.curId, permIds: this.permIds })
        // console.log(res)
        this.$message.success(res.message)
        // 刷新列表
        this.$emit('successAddPerMission')
        // 清空当前的选择
        this.$refs.treeRef.setCheckedKeys([])
      } catch (error) {
        this.$message.error(error.message)
      }
    }

  }
}
</script>

<style>

</style>
